
package com.android.picotts.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.picotts.R;
/*******************************************************
 * @author : zhaohua
 * @version: 2012-8-11
 * @see : 标题栏 
 * @Copyright: copyrights reserved by personal 2007-2011
*******************************************************/
public class TitleBar extends LinearLayout
{
    private ImageTextButton left_lay;
    private ImageTextButton right_lay;
    private LinearLayout title_lay;
    private TextView title_tv;
    
    public TitleBar(Context context)
    {
        super(context);
        initViews();
    }
    
    public TitleBar(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initViews();
    }
    
    /**　设置中间的标题文本*/
    public void setTitle(int resId)
    {
        title_tv.setText(resId);
    }
    /**　设置中间的标题文本*/
    public void setTitle(String resId)
    {
        title_tv.setText(resId);
    }
    
    /** 设置左边区域的背景*/
    public void setLeftBg(int bgId)
    {
        left_lay.setBg(bgId);
    }
    /** 设置右区域的背景*/
    public void setRigthBg(int bgId)
    {
        right_lay.setBg(bgId);
    }
    
    /** 设置左区域的图片,并隐藏文本*/
    public void setLeftImage(int imgId)
    {
        left_lay.setImage(imgId);
    }
    /** 设置右区域的图片,并隐藏文本*/
    public void setRigthImage(int imgId)
    {
        right_lay.setImage(imgId);
    }
    
    /** 设置左区域的文本,并隐藏图片*/
    public void setLeftText(int textId)
    {
        left_lay.setText(textId);
    }
    /** 设置右区域的文本,并隐藏图片*/
    public void setRigthText(int textId)
    {
        right_lay.setText(textId);
    }
    
    /** 设置左区域的文本,并隐藏图片*/
    public void setLeftText(String textId)
    {
        left_lay.setText(textId);
    }
    /** 设置右区域的文本,并隐藏图片*/
    public void setRigthText(String textId)
    {
        right_lay.setText(textId);
    }
    
    public TextView getLeftTextView()
    {
        return left_lay.getTextView();
    }
    
    public TextView getRightTextView()
    {
        return right_lay.getTextView();
    }
    
    public ImageView getLeftImgView()
    {
        return left_lay.getImageView();
    }
    
    public ImageView getRightImgView()
    {
        return right_lay.getImageView();
    }
    
    /** 设置左区域的可见性*/
    public void setLeftVisible(boolean visible)
    {
        left_lay.setVisibility(visible ? View.VISIBLE: View.GONE);
    }
    
    /** 设置右区域的可见性*/
    public void setRightVisible(boolean visible)
    {
        right_lay.setVisibility(visible ? View.VISIBLE: View.GONE);
    }

    /** 重置左边区域的视图*/
    public void setCustomLeftView(View customView)
    {
        left_lay.setCustomView(customView);
    }
    
    /** 重置右边区域的视图*/
    public void setCustomRightView(View customView)
    {
        right_lay.setCustomView(customView);
    }
    
    /** 重置中间区域的视图*/
    public void setCustomCenterView(View customView)
    {
        title_lay.removeAllViews();
        title_lay.addView(customView);
    }
    
    /** 设置左右按钮的事件监听器
     *  @param leftenable 左按钮是否设置监听，默认是设置了finish的监听动作的，
     *                     如果此为返回，则设置为false
     *  @param rightenable 右按钮是否设置监听，默认是没有设置任何监听动作的
     * */
    public void setOnTitleClickListener(OnTitleClickListener listener,
            boolean leftenable, boolean rightenable)
    {
        if(leftenable) left_lay.setOnClickListener(listener);
        if(rightenable) right_lay.setOnClickListener(listener);
    }
    
    private void initViews()
    {
        View row = inflate(getContext(), R.layout.view_title_bar, null);
        addView(row,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        left_lay = (ImageTextButton)row.findViewById(R.id.left_lay);
        right_lay = (ImageTextButton)row.findViewById(R.id.right_lay);
        title_lay = (LinearLayout)row.findViewById(R.id.title_lay);
        title_tv = (TextView)row.findViewById(R.id.title_tv);
        // 默认给左边设置为 返回动作按钮
        left_lay.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Context context = getContext();
                if(context instanceof Activity)
                {
                    ((Activity)context).finish();
                }
            }
        });
    }

    public static abstract class OnTitleClickListener implements OnClickListener
    {
        public void onLeftClick(){};
        public abstract void onRightClick();
        
        @Override
        public void onClick(View v)
        {
            switch(v.getId())
            {
                case R.id.left_lay:
                {
                    onLeftClick();
                    break;
                }
                case R.id.right_lay:
                {
                    onRightClick();
                    break;
                }
            }
        }
    }
}